import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_jd/ui/widgets/kit.dart';
import 'package:get/route_manager.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class TopBar extends StatefulWidget implements PreferredSizeWidget {
  final PreferredSizeWidget? bottom;
  final String? title;
  final List<Widget>? actions;
  final TextStyle? titleStyle;
  final Color? backgroundColor;
  final String? backImgName;
  final bool? isBack;
  final Brightness? brightness;
  final double height;
  final Color color;
  TopBar({
    this.bottom,
    this.title,
    this.actions,
    this.titleStyle,
    this.backgroundColor,
    this.backImgName,
    this.isBack: false,
    this.brightness,
    this.height: 45,
    this.color: Colors.black,
  });
  @override
  _TopBarState createState() => _TopBarState();

  @override
  Size get preferredSize =>
      Size.fromHeight(this.bottom != null ? 45.h : this.height.h);
}

class _TopBarState extends State<TopBar> {
  @override
  Widget build(BuildContext context) {
    return AppBar(
      centerTitle: true, // 标题居中
      brightness: widget.brightness,
      leading: GestureDetector(
        onTap: () {
          if (widget.isBack == true) {
            Get.back();
          }
        },
        child: Container(
          child: Offstage(
            offstage: widget.isBack == true ? false : true,
            child: Container(
              width: 30,
              color: Colors.transparent,
              padding: EdgeInsets.only(left: 5.w, right: 10.w),
              child: Kit.iconFont(0xe671, widget.color, 18.sp),
            ),
          ),
        ),
      ),
      title: new Text(
        widget.title ?? '',
        // style: TextStyle(
        //   color: Color(0xFF333333),
        //   fontSize: 17,
        // ),
        style: widget.titleStyle ??
            TextStyle(
              color: Color(0xFF333333),
              fontSize: 17.0.sp,
            ),
      ),
      backgroundColor: widget.backgroundColor ?? Color(0xFFFFFFFF),
      elevation: 0,
      bottom: widget.bottom,
      actions: widget.actions,
    );
  }
}

class AppBarBottom extends StatelessWidget implements PreferredSizeWidget {
  final Widget? child;
  AppBarBottom({
    this.child,
  });
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          Container(
            width: double.infinity,
            height: 1,
            color: Color(0xFFE5E5E5),
          ),
          child ??
              SizedBox(
                height: 0,
              ),
        ],
      ),
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(this.child != null ? 47 : 1);
}
